<template>
  <section>
    <el-checkbox-group v-model="member.userResource.companies" v-if="companies">
      <el-checkbox v-for="company in companies" :label="company.id" :key="company.id" @change="updateCompanyPermission($event,company)">{{company.companyName}}</el-checkbox>
    </el-checkbox-group>
  </section>
</template>

<script>
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "companies",
    props:['member'],
    data(){
      return {
        companies:null
      }
    },
    created(){
      if(this.member.userResource.companies === undefined){
        Vue.set(this.member.userResource,'companies',[])
      }
      this.getCompanyList()
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      getCompanyList(){
        this.fetch({name:"getCompanyList"})
          .then(res=>{
            this.companies = res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      updateCompanyPermission(e,c){
        console.log(e,c)
        let value;
        if(e) value = 1
        else value = 0
        this.fetch({name:"setUserCompany",data:{
            companyId:c.id,
            type:value,
            userId:this.member.id
          }})
          .then(res=>{
            console.log(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
    }
  }
</script>

<style scoped>

</style>
